﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VOL;
using BLL;
using System.Data;
using System.Data.SqlClient;

namespace WebShoes.Admin
{
    public partial class HangHoaManagerForm : System.Web.UI.Page
    {
        int chon =  0;
        int search = 0;
        HangHoaBL _hangHoaBL = new HangHoaBL();
        ChungLoaiBL _chungLoaiBL = new ChungLoaiBL();
        LoaiHangHoaBL _loaiHangHoaBL = new LoaiHangHoaBL();
        NhaSanXuatBL _nhaSanXuatBL = new NhaSanXuatBL();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            fillData();
        }

        private void fillData()
        {
            if (chon == 0 || chon == 1 || chon == 2)
            {
                ddlSearch.Visible = false;
                txtNhap.Visible = true;
            }
            else
            {
                ddlSearch.Visible = true;
                txtNhap.Visible = false;
            }


            fillDataSearch(_hangHoaBL.GetDataGrip());
        }

        private void fillDataSearch(DataTable _dt)
        {
            GridView1.DataSource = _dt;
            GridView1.DataBind();
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            chon = int.Parse(ddlChon.SelectedValue);
            try
            {
                search = int.Parse(ddlSearch.SelectedValue);
            }
            catch (Exception ex) { search = 0; }
            if (txtNhap.Text == "" && (chon == 1 || chon == 2))
                lblThongBao.Text = "Vui lòng nhập thông tin tìm kiếm";
            if (ddlSearch.SelectedIndex == 0 && (chon == 3 || chon == 4 || chon == 5))
                lblThongBao.Text = "Vui lòng chọn dữ liệu cần tìm kiếm";

            //Tìm kiếm theo tên hàng hóa
            if (txtNhap.Text != "" && chon == 2)
            {
                string nhap = txtNhap.Text;
                dt = _hangHoaBL.searchTenHangHoa(txtNhap.Text.Trim());
                fillDataSearch(dt);
            }

            //Tìm kiếm theo mã hàng hóa
            if (txtNhap.Text != "" && chon == 1)
            {
                int nhap;
                try {
                    nhap = int.Parse(txtNhap.Text.Trim());
                    lblThongBao.Text = "";
                    dt = _hangHoaBL.searchMaHangHoa(nhap);
                    fillDataSearch(dt);
                }
                catch (Exception ex) { lblThongBao.Text = "Vui lòng nhập số"; }
            }

            //Tìm theo chủng loại hàng
            if (chon == 3)
            {
                if (search != 0)
                {
                    dt = _hangHoaBL.searchMaChungLoai(search);
                    fillDataSearch(dt);
                }
            }

            //Tìm theo nhà sản xuất
            if (chon == 4)
            {
                if (search != 0)
                {
                    dt = _hangHoaBL.searchNhaSanXuat(search);
                    fillDataSearch(dt);
                }
            }

            //Tìm theo loại hàng hóa
            if (chon == 5)
            {
                if (search != 0)
                {
                    dt = _hangHoaBL.searchLoaiHangHoa(search);
                    fillDataSearch(dt);
                }
            }

            //Giữ nguyên hiện trường
            loadButton();
            ddlChon.SelectedValue = chon.ToString();
            try
            {
                ddlSearch.SelectedValue = search.ToString();
            }
            catch (Exception ex) { search = 0; }
        }

        private void loadButton()
        {
            ddlChon.SelectedValue = chon.ToString();
            if (chon == 0 || chon == 1 || chon == 2)
            {
                ddlSearch.Visible = false;
                txtNhap.Visible = true;                
            }
            else
            {
                ddlSearch.Visible = true;
                txtNhap.Visible = false;
            }

            if (chon == 3)
            {
                ddlSearch.DataSource = _chungLoaiBL.GetData();
                ddlSearch.DataValueField = "MaChungLoai";
                ddlSearch.DataTextField = "TenChungLoai";
                ddlSearch.DataBind();
                ddlSearch.Items.Insert(0, "--Vui lòng chọn--");
                return;
            }
            if (chon == 4)
            {
                ddlSearch.DataSource = _nhaSanXuatBL.GetData();
                ddlSearch.DataValueField = "MaNhaSanXuat";
                ddlSearch.DataTextField = "TenNhaSanXuat";
                ddlSearch.DataBind();
                ddlSearch.Items.Insert(0, "--Vui lòng chọn--");
                return;
            }
            ddlSearch.DataSource = _loaiHangHoaBL.GetData();
            ddlSearch.DataValueField = "MaLoaiHangHoa";
            ddlSearch.DataTextField = "TenLoaiHangHoa";
            ddlSearch.DataBind();
            ddlSearch.Items.Insert(0, "--Vui lòng chọn--");
        }

        protected void ddlChon_SelectedIndexChanged(object sender, EventArgs e)
        {            
            chon = int.Parse(ddlChon.SelectedValue);
            loadButton();
        }

        protected void ddlSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            //chon = int.Parse(ddlChon.SelectedValue);
            //search = int.Parse(ddlSearch.SelectedValue);
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdDelete")
            {
                int maHangHoa = int.Parse(e.CommandArgument.ToString());
                HangHoa obj = new HangHoa();
                obj.MaHangHoa = maHangHoa;
                _hangHoaBL.deleteHangHoa(obj);
                fillData();
            }
        }


    }
}